package cn.gtv.sdk.dcas.api;

import android.content.Context;

import com.sad.jetpack.v1.datamodel.api.DataModelProducerImpl;
import com.sad.jetpack.v1.datamodel.api.IDataModelInterceptorInput;
import com.sad.jetpack.v1.datamodel.api.IDataModelProducer;
import com.sad.jetpack.v1.datamodel.api.IDataModelRequest;
import com.sad.jetpack.v1.datamodel.api.extension.client.DataClient;
import com.sad.jetpack.v1.datamodel.api.extension.engine.OkHttpEngineForStringByStringBody;
import com.sad.jetpack.v1.datamodel.api.extension.interceptor.DefaultStringCacheDataModelInterceptor;
import com.sad.jetpack.v1.datamodel.api.extension.interceptor.LogDataModelInterceptor;
import com.sad.jetpack.v1.datamodel.api.extension.interceptor.SimpleStringCacheDataModelInterceptor;

public abstract class BaseHttpDCASDataClient<C extends BaseHttpDCASDataClient<C>> extends DataClient<C,String> {
    private Context context;

    public Context getContext() {
        return context;
    }

    protected BaseHttpDCASDataClient(Context context){
        this.context=context;
    }
    @Override
    public IDataModelProducer<String> dataModelProducer(String tagAndClientKey) {
        IDataModelRequest request=requestCreator.tag(tagAndClientKey).create();
        LogDataModelInterceptor logInterceptor = LogDataModelInterceptor.newInstance();
        SimpleStringCacheDataModelInterceptor cacheInterceptor = new SimpleStringCacheDataModelInterceptor();
        IDataModelProducer<String> dataModelProducer = DataModelProducerImpl.<String>newInstance();
        dataModelProducer.addInputInterceptor(protocolInterceptorInput(tagAndClientKey));
        if (isInternalLog()){
            dataModelProducer.addInputInterceptor(logInterceptor).addOutputInterceptor(logInterceptor);
        }
        if (request.method()== IDataModelRequest.Method.GET){
            dataModelProducer.addInputInterceptor(cacheInterceptor).addOutputInterceptor(cacheInterceptor);
        }
        dataModelProducer
                .newRequest(request)
                .engine(new OkHttpEngineForStringByStringBody());
        return dataModelProducer;
    }

    public abstract IDataModelInterceptorInput<String> protocolInterceptorInput(String tagAndClientKey);
}
